/*
 * serialize.cpp
 *
 *  Created on: 2012-9-26
 *      Author: Administrator
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <libxml/parser.h>

#include "serialize.hpp"
#include "./cpp/cppSerialize.hpp"
#include "./cs/csSerialize.hpp"

void toUpperCase( char* str ) {
	while( *str ) {
		*str = toupper( *str ) ;
		++str ;
	}
}
//void firstToUpperCase( char* str ) {
//	str[0] = toupper( str[0] ) ;
//}
//void firstToUpperCase2( const char* str , char* to ) {
//	strcpy( to , str ) ;
//	firstToUpperCase( to ) ;
//}


/*
 * 解析 type 段
 */
int makeType( ProtocolModule* module , xmlNode* node ) {
	ProtocalType *type = new ProtocalType() ;
	char *pWrite ;

	pWrite = xmlGetProp( node , "name" ) ;
	if( pWrite == NULL ) {
		printf( "错误：没有定义类型名[%s]\n" , module->name ) ;
		return 1 ;
	}
	strcpy( type->name , xmlGetProp( node , "name" ) ) ;
//	firstToUpperCase( type->name ) ;
//	xmlSetProp( node , "name" , type->name ) ;

	/*
	 * 生成头文件
	 */
	pWrite = workBuff ;
	pWrite += makeHeader( pWrite , node , SERIALIZE_TYPE_TYPE ) ;
	type->hppSize = pWrite - workBuff ;
	type->hppContent = new char[type->hppSize + 1] ;
	memcpy( type->hppContent , workBuff , pWrite - workBuff +1 ) ;

	/**
	 *  生成源文件
	 */
	pWrite = workBuff ;
	// 构造函数
	pWrite += makeConstructor( pWrite , node ) ;
	// 析构函数
	pWrite += makeDestruction( pWrite , node ) ;
	// 序列化函数
	pWrite += makeSerialize( pWrite , node , SERIALIZE_TYPE_TYPE ) ;
	// 反序列化函数
	pWrite += makeUnserialize( pWrite , node ) ;
	type->cppSize = pWrite - workBuff ;
	type->cppContent = new char[type->cppSize + 1] ;
	memcpy( type->cppContent , workBuff , pWrite - workBuff +1 ) ;

	/**
	 * 生成 C# 的 info 文件
	 */
	pWrite = workBuff ;
	pWrite += makeCsMember( pWrite , node ) ;
	pWrite += makeCsSerialize( pWrite , node , SERIALIZE_TYPE_TYPE ) ;
	pWrite += makeCsUnserialize( pWrite , node ) ;
	type->csSize = pWrite - workBuff ;
	type->csContent = new char[type->csSize + 1] ;
	memcpy( type->csContent , workBuff , pWrite - workBuff +1 ) ;

	type->next = module->types ;
	module->types = type ;


	return 0 ;
}
/**
 * 解析 request 段
 */
int makeRequest( ProtocolModule* module , xmlNode* node ) {
	ProtocalRequester *requester = new ProtocalRequester() ;
	char *pWrite ;

	pWrite = xmlGetProp( node , "id" ) ;
	if( pWrite == NULL ) {
		printf( "错误：没有定义消息ID[%s]\n" , module->name ) ;
		return 1 ;
	}
	requester->id = atoi(  xmlGetProp( node , "id" ) ) ;
	pWrite = xmlGetProp( node , "name" ) ;
	if( pWrite == NULL ) {
		printf( "错误：没有定义消息名[%s]\n" , module->name ) ;
		return 1 ;
	}
	strcpy( requester->name , xmlGetProp( node , "name" ) ) ;
//	firstToUpperCase(  requester->name ) ;
//	xmlSetProp( node , "name" , requester->name ) ;
	strcpy( requester->notify , requester->name ) ;
	toUpperCase( requester->notify ) ;

	/*
	 * 生成头文件
	 */
	pWrite = workBuff ;
	pWrite += makeHeader( pWrite , node , SERIALIZE_TYPE_REQUEST) ;
	requester->hppSize = pWrite - workBuff ;
	requester->hppContent = new char[requester->hppSize + 1] ;
	memcpy( requester->hppContent , workBuff , requester->hppSize +1 ) ;


	/**
	 *  生成源文件
	 */
	pWrite = workBuff ;
	pWrite += sprintf( pWrite ,
			"const uint16_t %s::messageId = %d ;\n" ,
			requester->name , requester->id ) ;
	// 构造函数
	pWrite += makeConstructor( pWrite , node ) ;
	// 析构函数
	pWrite += makeDestruction( pWrite , node ) ;
	// 反序列化函数
	pWrite += makeUnserialize( pWrite , node ) ;
	requester->cppSize = pWrite - workBuff ;
	requester->cppContent = new char[requester->cppSize + 1] ;
	memcpy( requester->cppContent , workBuff , pWrite - workBuff +1 ) ;

	/**
	 * 生成 C# 的 info 文件
	 */
	pWrite = workBuff ;
	pWrite += makeCsMember( pWrite , node ) ;
	pWrite += makeCsSerialize( pWrite , node , SERIALIZE_TYPE_TYPE ) ;
	requester->csSize = pWrite - workBuff ;
	requester->csContent = new char[requester->csSize + 1] ;
	memcpy( requester->csContent , workBuff , pWrite - workBuff +1 ) ;

	requester->next = module->requesters;
	module->requesters = requester ;

	return 0 ;
}
/**
 * 解析 respond 段
 */
int makeRespond( ProtocolModule* module , xmlNode* node ) {
	ProtocalRespond *respond = new ProtocalRespond() ;
	char *pWrite ;

	respond->id = atoi(  xmlGetProp( node , "id" ) ) ;
	strcpy( respond->name , xmlGetProp( node , "name" ) ) ;
//	firstToUpperCase( respond->name ) ;
//	xmlSetProp( node , "name" , respond->name ) ;
	strcpy( respond->notify , respond->name ) ;
	toUpperCase( respond->notify ) ;

	/*
	 * 生成头文件
	 */
	pWrite = workBuff ;
	pWrite += makeHeader( pWrite , node , SERIALIZE_TYPE_RESPOND ) ;
	respond->hppSize = pWrite - workBuff ;
	respond->hppContent = new char[respond->hppSize + 1] ;
	memcpy( respond->hppContent , workBuff , pWrite - workBuff +1 ) ;


	/**
	 *  生成源文件
	 */
	pWrite = workBuff ;
	pWrite += sprintf( pWrite ,
			"const uint16_t %s::messageId = %d ;\n\n" ,
			respond->name , respond->id ) ;
	// 构造函数
	pWrite += makeConstructor( pWrite , node ) ;
	// 析构函数
	pWrite += makeDestruction( pWrite , node ) ;
	// 序列化函数
	pWrite += makeSerialize( pWrite , node , SERIALIZE_TYPE_RESPOND) ;
	respond->cppSize = pWrite - workBuff ;
	respond->cppContent = new char[respond->cppSize + 1] ;
	memcpy( respond->cppContent , workBuff , pWrite - workBuff +1 ) ;

	/**
	 * 生成 C# 的 info 文件
	 */
	pWrite = workBuff ;
	pWrite += makeCsMember( pWrite , node ) ;
	pWrite += makeCsUnserialize( pWrite , node ) ;
	respond->csSize = pWrite - workBuff ;
	respond->csContent = new char[respond->csSize + 1] ;
	memcpy( respond->csContent , workBuff , pWrite - workBuff +1 ) ;

	respond->next = module->responds ;
	module->responds = respond ;

	return 0 ;
}


































